package sk.stuba.fiit.npr.la;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLParser extends DefaultHandler {
	private StringBuffer buff = new StringBuffer();
	private Map<String, String> parsedSimpleURL = new HashMap<String, String>();
	private String id = null;

	public XMLParser() {
		super();
	}

	public void characters(char[] buffer, int start, int length)
			throws SAXException {
		this.buff.append(buffer, start, length);
	}

	public void startElement(String uri, String simpleName, String qName,
			Attributes attributes) throws SAXException {
		this.buff.setLength(0);
		this.id = attributes.getValue("id");
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (!"examples".equals(qName)) {
			parsedSimpleURL.put(qName, this.buff.toString());
		}
	}

	public Map<String, String> getParsedSimpleURL() {
		return parsedSimpleURL;
	}

	public void startParse(File file) throws ParserConfigurationException,
			SAXException, IOException {
		SAXParserFactory spf = SAXParserFactory.newInstance();
		spf.setValidating(false);
		SAXParser sp = spf.newSAXParser();
		sp.parse(file, this);
	}
}
